﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Game.Utilities.Config;
using Game.Utilities.Log;
using Game.Utilities.Session;
using VTCeBank.SSO.Utils;

namespace Web.Server
{
    [Serializable]
    public class AccountInfoWithBalance
    {
        public string AccessToken { get; set; }
        public int GameBalance { get; set; }
        public int VcoinBalance { get; set; }
        public int TotalGameBalance { get; set; }
    }

    public partial class login : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            RetrieveLoginInfo();
        }

        private void RetrieveLoginInfo()
        {
            try
            {
                //Account(request, type); type = 1 : GetKeyFromSSOServer other : GetKeyFromFacebookServer
                var account = new Account(Request, 1);
                //If status equals 0 then return url
                if (!account.IsLogin) Response.Redirect(account.ReturnURL);
                FormsAuthentication.SetAuthCookie(AccountSession.SessionName(long.Parse(account.AccountID), account.AccountName), false);


                string requestToken = Request["requestToken"];

                if (requestToken != null)
                {
                    //Config.AccessToken = account.GetAccessToken(requestToken);
                    string resp = account.GetAccessTokenWithBalance(requestToken);
                    AccountInfoWithBalance objJson = Newtonsoft.Json.JsonConvert.DeserializeObject<AccountInfoWithBalance>(resp);
                    AccountSession.AccessToken = objJson.AccessToken;
                    AccountSession.AccountTotalBalance = objJson.TotalGameBalance;
                }

                Response.Redirect(Config.ApplicationUrl, false);
            }
            catch (Exception ex)
            {
                NLogManager.LogMessage(ex.ToString());
                Response.Redirect("~/");
            }
        }
    }
}